/**
 * zhanjun
 */

$(function() {
	
	var idGroup=getUrlParamValue("idGroup");
	if(idGroup==""){
		alert("传入的参数不正确，请重新打开！");
		location.href='group-admin.html';
	}
	$("#idGroup").attr("value",idGroup);
	$.getJSON("../group/ser_load.do?id="+idGroup, function(data) {	
		$('#sp_groupname').html(data.groupName);	
	});
	
	/*
	 * 初始化类型下拉框
	 */
	$("#idsystem")[0].options.length = 0;
	$.getJSON("../group/ser_getUserSystem.do", function(data) {
		$("<option value=''>--请选择--</option>").appendTo("#idsystem");
		$.each(data.options, function(i) {
			$("<option value='" + data.options[i].key + "'>"+ data.options[i].value + "</option>").appendTo("#idsystem");
		});
	});
	
	var loadRoleSelect=function(idsystem){
		$("#SrcSelect")[0].options.length = 0;
		$.getJSON("../group/ser_getUserRole.do?idSystem="+idsystem, function(data) {			
			$.each(data.options, function(i) {
				$("<option value='" + data.options[i].key + "'>"+ data.options[i].value + "</option>").appendTo("#SrcSelect");
			});
		});
	};
	
	$('#idsystem').bind('change', function() {
		if($("#idsystem").val()!=""){
			var idsys=$("#idsystem").val();
			loadRoleSelect(idsys);
		}
	});

	

	
	

	$("#btn_reset").click(function() {
		document.getElementById("dataform").reset();

		location.href='group-admin.html';
		
	});
	/*
	 * 保存事件
	 */

	$("#btn_submit").click(function() {
		var postdata = "idGroup="+idGroup;
		var ObjSelect =document.getElementById("ObjSelect");
		var itemField=new Array();
		var itemName=new Array();
		if(ObjSelect&&ObjSelect.options&&ObjSelect.options.length>0){
			var len = ObjSelect.options.length;
			for(var j=0; j<len; j++){
				itemField.push(ObjSelect.options[j].value);
				itemName.push(ObjSelect.options[j].text);
			}
		}
		postdata += "&idRole="+itemField.join(",");
			
			$.ajax({
				type : "POST",
				url : "../group/ser_groupRoleSave.do",
				data : postdata,
				success : function(msg) {
					alert(msg);
					location.reload();					
				}
			});
		
	});

});
